package com.speedata.utils;


import android.annotation.SuppressLint;
import android.util.Log;

import com.speedata.MyApp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xu
 * @date 2022 07 14
 */
public class FileUtils {


    //读取文本文件中的内容
    public static int importTxtFile(String strFilePath) {

        @SuppressLint("SdCardPath") String path = "/sdcard/" + strFilePath;

        List<String> listResult = new ArrayList<>();
        //打开文件
        File file = new File(path);

        try {
            InputStream instream = new FileInputStream(file);
            InputStreamReader inputreader = new InputStreamReader(instream, "GBK"); //编码
            BufferedReader buffreader = new BufferedReader(inputreader);
            String line;
            //分行读取
            for (int i = 1; i <= 100000; i++) {
                //预留10W条
                while ((line = buffreader.readLine()) != null) {
                    listResult.add(line);
                }
            }
            instream.close();

        } catch (java.io.FileNotFoundException e) {
            Log.d("TestFile", "The File doesn't not exist.");
            return -1;
        } catch (IOException e) {
            Log.d("TestFile", e.getMessage());
        }

        MyApp.getInstance().setmList(listResult);

        return 1;
    }


    /**
     * 盘点单列表--导出
     *
     * @param list     outlist
     * @param filename 2.txt
     * @return success
     */
    public int outputfile(List<String> list, String filename) {

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filename));
            for (int i = 0; i < list.size(); i++) {
                //拿字符串
                String str = list.get(i);

                String over = str + "\r\n";
                // 写文件
                bw.write(over, 0, over.length());
                // 刷新流
                bw.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭文件流
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }


}








